package com.linkwechat.web.controller.wecom;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.enums.MediaType;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WePoster;
import com.linkwechat.wecom.domain.dto.WeChatItemDto;
import com.linkwechat.wecom.domain.vo.WeChatSideTransferAllVo;
import com.linkwechat.wecom.domain.vo.WeChatSideTransferVo;
import com.linkwechat.wecom.domain.vo.WeChatSideVo;
import com.linkwechat.wecom.service.IWeCategoryService;
import com.linkwechat.wecom.service.IWeChatItemService;
import com.linkwechat.wecom.service.IWePosterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天工具侧边栏
 *
 * @author kewen
 */
@RequestMapping(value = "/wecom/chat/item")
@RestController
public class WeChatItemController extends BaseController {

    @Autowired
    private IWeChatItemService weChatItemService;
    @Resource
    private IWePosterService wePosterService;

    @Resource
    private IWeCategoryService weCategoryService;

    /**
     * 侧边栏抓取素材
     */
//    @PreAuthorize("@ss.hasPermi('chat:item:add')")
   @Log(title = "侧边栏抓取素材", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult add(@RequestBody WeChatItemDto chatItemDto) {
        return toAjax(weChatItemService.checkItems(chatItemDto));
    }

    /**
     * h5素材列表
     */
    //@PreAuthorize("@ss.hasPermi('chat:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "sideId") Long sideId
            , @RequestParam(value = "keyword", required = false) String keyword
            ,@RequestParam(value = "mediaType") String mediaType
            ,@RequestParam(value = "userId") String userId
            ,@RequestParam(value = "topId") Long topId) {
        startPage();
        List<WeChatSideVo> weChatSideVos=null;
        if(StringUtils.isNotBlank(mediaType)&&mediaType.equals(MediaType.POSTER.getType())){
            weChatSideVos=wePosterService.list(keyword).stream().map(wePoster -> {
               WeChatSideVo weChatSideVo=new WeChatSideVo();
               weChatSideVo.setMediaType(MediaType.POSTER.getType());
               weChatSideVo.setMaterialName(wePoster.getTitle());
               weChatSideVo.setMaterialUrl(wePoster.getSampleImgPath());

               weChatSideVo.setMaterialId(wePoster.getId());
               weChatSideVo.setCoverUrl(wePoster.getBackgroundImgPath());
               weChatSideVo.setContent(wePoster.getTitle());

               return weChatSideVo;
            }).collect(Collectors.toList());

        }else{
            weChatSideVos = weChatItemService.chatItems(sideId,keyword,mediaType,userId,topId);
        }
         return getDataTable(weChatSideVos);
    }

    @GetMapping("/getTransferList")
    public AjaxResult getTransferList(@RequestParam(value = "sideId") Long sideId
            , @RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "mediaType") String mediaType, @RequestParam(value = "userId") String userId){

        List<Long> weChatSideTransferVos=null;
        List<WeChatSideTransferVo> weChatSideTransferVosNotSelect=null;

        if(StringUtils.isNotBlank(mediaType)&&mediaType.equals(MediaType.POSTER.getType())){

            weChatSideTransferVos=wePosterService.findChatItemsBySelected(sideId,keyword,mediaType,userId);

            weChatSideTransferVosNotSelect=wePosterService.list(keyword).stream().map(wePoster -> {
                WeChatSideTransferVo weChatSideTransferVo=new WeChatSideTransferVo();
                weChatSideTransferVo.setMediaType(MediaType.POSTER.getType());
                weChatSideTransferVo.setMaterialId(wePoster.getId());
                weChatSideTransferVo.setMaterialName(wePoster.getTitle());
                weChatSideTransferVo.setMaterialUrl(wePoster.getSampleImgPath());
                weChatSideTransferVo.setCoverUrl(wePoster.getBackgroundImgPath());
                weChatSideTransferVo.setContent(wePoster.getTitle());

                return weChatSideTransferVo;
            }).collect(Collectors.toList());

        }else{
            // 获取分类已选素材
            weChatSideTransferVos=weChatItemService.chatItemsSelectedByTransfer(sideId,keyword,mediaType,userId);
            // 获取分类待选素材
            weChatSideTransferVosNotSelect=weChatItemService.chatItemsNotSelectedByTransfer(sideId,keyword,mediaType,userId);

        }

        WeChatSideTransferAllVo weChatSideTransferAllVo=new WeChatSideTransferAllVo();
        weChatSideTransferAllVo.setSelected(weChatSideTransferVos);
        weChatSideTransferAllVo.setNoSelected(weChatSideTransferVosNotSelect);

        return AjaxResult.success(weChatSideTransferAllVo);

    }


    @ApiOperation("查询素材类型下一级分类")
    @GetMapping("/findOneLevelWeCategories")
    public AjaxResult findOneLevelWeCategories(@RequestParam("mediaType") String mediaType){
        return AjaxResult.success(weCategoryService.findOneLevelWeCategory(mediaType));
    }

}
